﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using VOL;
using System.Data;

namespace Web.Admin
{
    public partial class HoaDonBanForm : System.Web.UI.Page
    {
        HoaDonBanBL _HoaDonBanBL = new HoaDonBanBL();
        int _MaHoaDon = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["MaHD"] != null)
            {
                int.TryParse(Request.QueryString["MaHD"], out _MaHoaDon);

                if(_MaHoaDon > 0)
                {
                    DataTable dt = _HoaDonBanBL.getDataReport(_MaHoaDon);
                    if (dt.Rows.Count > 0)
                    {
                        lblDienGiai.Text = dt.Rows[0]["DienGiai"].ToString();
                        lblDiaChi.Text = dt.Rows[0]["DiaChi"].ToString();
                        lblDienThoai.Text = dt.Rows[0]["DienThoai"].ToString();
                        lblEmail.Text = dt.Rows[0]["Email"].ToString();
                        lblHoTen.Text = dt.Rows[0]["HoTen"].ToString();
                        lblMaHD.Text = dt.Rows[0]["MaHD"].ToString();
                        lblNgHD.Text = dt.Rows[0]["NgHD"].ToString();
                        lblTongTien.Text = dt.Rows[0]["ThanhTien"].ToString();

                        btnSave.Visible = true;
                        if (dt.Rows[0]["Duyet"].ToString() == "True")
                        {
                            lblDuyet.Text = "Đã Giao Hàng";
                            lblNgDuyet.Text = dt.Rows[0]["NgDuyet"].ToString();
                            btnSave.Text = "Hủy Giao Hàng";
                            hdfDuyet.Value = "True";
                        }
                        else
                        {
                            lblDuyet.Text = "Chưa Giao Hàng";
                            lblNgDuyet.Text = "";
                            btnSave.Text = "Đã Giao Hàng";
                        }

                        ChiTietHoaDonBL chiTietBl = new ChiTietHoaDonBL();
                        DataTable dtChiTiet = chiTietBl.getDataReport(_MaHoaDon);
                        GridView1.DataSource = dtChiTiet;
                        GridView1.DataBind();
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int.TryParse(lblMaHD.Text, out _MaHoaDon);
            if (_MaHoaDon > 0)
            {
                HoaDonBan obj = new HoaDonBan();
                obj.MaHD = _MaHoaDon;
                obj.NgDuyet = DateTime.Now;
                // duyệt hoặc hủy duyệt đơn hàng
                if (hdfDuyet.Value == "True")
                {
                    // hủy
                    obj.Duyet = false;
                }
                else
                { 
                    // duyệt
                    obj.Duyet = true;
                }
                _HoaDonBanBL.updateDuyetHoaDon(obj);
            }
        }
    }
}